package com.tydic.portal.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tydic.common.core.domain.R;
import com.tydic.common.mybatis.core.page.TableDataInfo;
import com.tydic.product.domain.CarNumber;
import com.tydic.product.domain.request.CarNumberPageRequest;
import com.tydic.product.domain.request.CarNumberRequest;
import com.tydic.product.domain.response.CarNumberResponse;
import com.tydic.product.service.ICarNumberService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/portal/carNumber")

public class CarNumberController {
    private final ICarNumberService carNumberService;

    /**
     * 保存车辆信息
     */
    @PostMapping("/save")
    public R save(@Valid @RequestBody CarNumberRequest carNumberRequest) {
        carNumberService.save(carNumberRequest.getBusinessId(), carNumberRequest.getBusinessType(), carNumberRequest.getCarNumber());
        return R.ok();
    }

    /**
     * 车辆信息列表
     */
    @PostMapping("/list")
    public R list(@Valid @RequestBody CarNumberRequest carNumberRequest) {
        List<CarNumberResponse> carNumbers = carNumberService.list(carNumberRequest.getBusinessId(), carNumberRequest.getBusinessType(), DateUtil.today()).stream().map(item -> {
            CarNumberResponse carNumberResponse = new CarNumberResponse();
            BeanUtils.copyProperties(item, carNumberResponse);
            return carNumberResponse;
        }).collect(Collectors.toList());
        return R.ok(carNumbers);
    }


    /**
     * 车辆信息分页列表
     */
    @PostMapping("/page")
    public R page(@Valid @RequestBody CarNumberPageRequest carNumberPageRequest) {
        Page<CarNumber> page = carNumberService.page(carNumberPageRequest.getPageNo(), carNumberPageRequest.getPageSize());
        return R.ok(TableDataInfo.build(page));
    }
}
